package com.tucc.hello.leetcode.no1_400.no1_200.no1_20.no11_20.no20;

import java.util.Stack;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.isValid("{1(2)}"));
        System.out.println(solution.isValid("{1(2}"));
        System.out.println(solution.isValid("{1(2[)]}"));

    }

    public boolean isValid(String s) {
        Stack<Integer> stack = new Stack<>();
        for(char ch : s.toCharArray()){
            switch (ch){
                case '(' : stack.add((int)')');
                    break;
                case '[' : stack.add((int)']');
                    break;
                case '{' : stack.add((int)'}');
                    break;
                case ')':
                case ']':
                case '}':
                    if(stack.isEmpty()){
                        return false;
                    }
                    Integer last = stack.pop();
                    if(last == null || last.intValue() != (int) ch){
                        return false;
                    }
                    break;
            }
        }
        return stack.isEmpty();
    }

}
